package top.lyjwn.todo.aop.log.http;

import com.alibaba.fastjson2.annotation.JSONField;
import top.lyjwn.todo.aop.log.http.request.ServletRequestWrapper;
import top.lyjwn.todo.aop.log.http.response.ServletResponseWrapper;

import java.io.IOException;
import java.time.LocalDateTime;

public class HttpLogE {

    public HttpLogE() {
    }

    public HttpLogE setRequest(ServletRequestWrapper request) {
        this.setUrl(request.getRequestURI());
        String token = request.getHeader("token");
        this.setToken(token);
        this.setIp(request.getRemoteAddr());
        this.setMethod(request.getMethod());
        this.setRequestContentType(request.getHeader("Accept"));
        this.setRequestContent(request.getContentAsString());
        this.setBeginTime(LocalDateTime.now());
        return this;
    }

    public HttpLogE setResponse(ServletResponseWrapper response) throws IOException {
        String responseText = new String(response.getContentAsByteArray());
        response.copyBodyToResponse();
        this.setEndTime(LocalDateTime.now());
        this.setResponseContent(responseText);
        this.setResponseStatus(response.getStatus());
        this.setResponseContentType(response.getContentType());
        return this;
    }

    @JSONField(ordinal = 1)
    private String ip;

    @JSONField(ordinal = 2)
    private String url;

    @JSONField(ordinal = 3)
    private String method;

    @JSONField(ordinal = 4)
    private String token;

    @JSONField(ordinal = 5)
    private String requestContentType;

    @JSONField(ordinal = 6)
    private String requestContent;

    @JSONField(ordinal = 7)
    private int responseStatus;

    @JSONField(ordinal = 8)
    private String responseContentType;

    @JSONField(ordinal = 9)
    private String responseContent;

    @JSONField(ordinal = 10)
    private LocalDateTime beginTime;

    @JSONField(ordinal = 11)
    private LocalDateTime endTime;

    @JSONField(serialize = false, deserialize = false)
    public boolean isResponseBody() {
        return this.responseContentType != null && (this.responseContentType.toLowerCase().contains("json") || this.responseContentType.toLowerCase().contains("xml"));
    }


    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getRequestContentType() {
        return requestContentType;
    }

    public void setRequestContentType(String requestContentType) {
        this.requestContentType = requestContentType;
    }

    public String getRequestContent() {
        return requestContent;
    }

    public void setRequestContent(String requestContent) {
        this.requestContent = requestContent;
    }

    public int getResponseStatus() {
        return responseStatus;
    }

    public void setResponseStatus(int responseStatus) {
        this.responseStatus = responseStatus;
    }

    public String getResponseContentType() {
        return responseContentType;
    }

    public void setResponseContentType(String responseContentType) {
        this.responseContentType = responseContentType;
    }

    public String getResponseContent() {
        return responseContent;
    }

    public void setResponseContent(String responseContent) {
        this.responseContent = responseContent;
    }

    public LocalDateTime getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(LocalDateTime beginTime) {
        this.beginTime = beginTime;
    }

    public LocalDateTime getEndTime() {
        return endTime;
    }

    public void setEndTime(LocalDateTime endTime) {
        this.endTime = endTime;
    }
}
